// pages/updataAdd/index.js
const app = getApp()

Page({
  data: {
    region: ['浙江省', '杭州市', '滨江区'],
    customItem: '全部',
    addrData: {}
  },
  onLoad: function (options) {
    var userId = wx.getStorageSync('userId')
    var that = this;
    wx.request({
      url: app.globalData.basicUrl + 'user/userData/' + userId,
      success: function (res) {
        console.log(res);
        let addrData = res.data.data.userAddress
        that.data.region[0] = addrData.province
        that.data.region[1] = addrData.city
        that.data.region[2] = addrData.district
        that.setData({
          addrData: addrData,
          region: that.data.region


        })
      }
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit: function (e) {
    wx.showLoading({
      title: '正在提交',
      mask:true
    })
    console.log(e.detail.value);
    let data = e.detail.value;
    var adds = ''
    var userId = wx.getStorageSync('userId')
    data.userId = userId
    data.addressId = this.data.addrData.addressId
    data.province = this.data.region[0]
    data.city = this.data.region[1]
    data.district = this.data.region[2]
    let method=''
    if (this.data.addrData.addressId) {
      method='PUT'
    }else{
      method='POST'
    }
      wx.request({
        url: app.globalData.basicUrl + 'user/userAddress',
        method: method,
        data: data,
        success: function (res) {
          wx.hideLoading()
          wx.showToast({
            title: '修改成功',
            icon: "none",
            duration: 2000,
            complete(res){
              wx.navigateBack()
            }
          })
        },
        fail(res){
          wx.hideLoading()
          wx.showToast({
            title: '修改失敗',
            icon: "none",
            duration: 2000
          })
        }
      })
  },
})